/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.jiujiudevice.entity.JiuInkRecord;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderLog;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderRecord;
import com.boxin.ims.modules.jiujiudevice.service.JiuInkRecordService;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderLogService;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderRecordService;
import com.boxin.ims.modules.wechat.entity.JiuWechatMember;
import com.boxin.ims.modules.wechat.service.JiuWechatMemberService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 订单记录Controller
 * @author sxh
 * @version 2018-08-15
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuOrderRecord")
public class JiuOrderRecordController extends BaseController {

	@Autowired
	private JiuOrderRecordService jiuOrderRecordService;
	@Autowired
	private JiuOrderLogService jiuOrderLogService;
	@Autowired
	private JiuWechatMemberService jiuWechatMemberService;
	@Autowired
	private JiuInkRecordService jiuInkRecordService;
	
	@ModelAttribute
	public JiuOrderRecord get(@RequestParam(required=false) Long id) {
		if (id != null){
			return jiuOrderRecordService.get(id);
		}else{
			return new JiuOrderRecord();
		}
	}
	
	@RequiresPermissions("jiujiudevice:jiuOrderRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuOrderRecord jiuOrderRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		/*if (!user.isAdmin()){
			jiuOrderRecord.setUser(user);
		}*/
        Page<JiuOrderRecord> page = jiuOrderRecordService.find(new Page<JiuOrderRecord>(request, response), jiuOrderRecord); 
		List<JiuOrderRecord> list = page.getList();
        for (int i = 0; i < list.size(); i++) {
			JiuOrderRecord j = list.get(i);
			JiuOrderLog jiuOrderLog  =   jiuOrderLogService.findByOrderId(j.getId());
			if(jiuOrderLog != null && jiuOrderLog.getId() >0) {
				j.setJiuOrderLog(jiuOrderLog);
			}
			JiuWechatMember member = jiuWechatMemberService.get(j.getMemberId());
			if(member != null && member.getId() >0) {
				j.setJiuWechatMember(member);
			}
			/* 已废弃//获取墨盒记录表
			List<JiuInkRecord> inkRecord =  jiuInkRecordService.findByOrderNoAndDeviceSn(j.getOrderNo(),j.getDeviceSn());
			//设置到订单记录中
			j.setJiuInkRecord(inkRecord);
			System.out.println(j.getJiuInkRecordString()+"格式化的值");*/
		}
        model.addAttribute("page", page);
		return "modules/jiujiudevice/jiuOrderRecordList";
	}

	@RequiresPermissions("jiujiudevice:jiuOrderRecord:view")
	@RequestMapping(value = "form")
	public String form(JiuOrderRecord jiuOrderRecord, Model model) {
	
		User user = UserUtils.getUser();
	/*	if (jiuOrderRecord.getUser() == null || jiuOrderRecord.getUser().getId() <1){
			jiuOrderRecord.setUser(user);
		}
		*/
		model.addAttribute("jiuOrderRecord", jiuOrderRecord);
		return "modules/jiujiudevice/jiuOrderRecordForm";
	}

	@RequiresPermissions("jiujiudevice:jiuOrderRecord:edit")
	@RequestMapping(value = "save")
	public String save(JiuOrderRecord jiuOrderRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuOrderRecord)){
			return form(jiuOrderRecord, model);
		}
		jiuOrderRecordService.save(jiuOrderRecord);
		addMessage(redirectAttributes, "保存订单记录'" + jiuOrderRecord.getOrderNo()+ "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuOrderRecord/?repage";
	}
	
	@RequiresPermissions("jiujiudevice:jiuOrderRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuOrderRecordService.delete(id);
		addMessage(redirectAttributes, "删除订单记录成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuOrderRecord/?repage";
	}

}
